#include "task_test.h"
#include "app_main.h"

xTaskHandle xTask3Handle;


xData sendbuff[2]= \
{
	{num1, send1_id},
	{num2, send2_id}
};

void task_print(void * pvParameters)
{
	char *pcTaskName;
	volatile unsigned long ul;
	static portTickType xLastWakeTime;
	
	pcTaskName = (char *)pvParameters;
	for(;;)
	{

		printf(pcTaskName);

		vTaskDelay(250);
		printf("ulIdelCycleCount = %d \n",ulIdelCycleCount);
	
	}
}
void task_print1(void * pvParameters)
{
	char *pcTaskName;
	volatile unsigned long ul;
	static portTickType xLastWakeTime;
	unsigned portBASE_TYPE uxPriority;
	
	pcTaskName = (char *)pvParameters;
	uxPriority = uxTaskPriorityGet(NULL);
	//xTaskCreate(task3_delete,"Task3",configMINIMAL_STACK_SIZE * 5,NULL,3,&xTask3Handle);
	for(;;)
	{
		
		printf("Task1 is running\n");

		xTaskCreate(task3_delete,"Task3",configMINIMAL_STACK_SIZE * 3,NULL,3,&xTask3Handle);

		
		printf("next \n \n \n");
		vTaskDelay(300);
		
//		vTaskPrioritySet(xTask2Handle,uxPriority+1);
//		vTaskDelay(250);
//	for(ul=0;ul<15000000;)
//	{
//		ul++;
//	}

	}
}
void task3_delete(void *pvParameters)
{


	printf("hello Task3! \n");
	vTaskDelete(xTask3Handle); 
	vTaskDelay(250);
}
void task_print2(void * pvParameters)
{
	char *pcTaskName;
	volatile unsigned long ul;
	static portTickType xLastWakeTime;
	unsigned portBASE_TYPE uxPriority;
	uxPriority = uxTaskPriorityGet(NULL);
	
	pcTaskName = (char *)pvParameters;
	for(;;)
	{

		printf(pcTaskName);

		

		printf("return to  \n \n");
		vTaskPrioritySet(NULL,(uxPriority -2 ));
	//	for(ul=0;ul<15000000;)
	//	{
	//		ul++;
	//	}

		
	//	vTaskDelay(250);
		

	}
}
void task_sender(void *pvParameters)
{
	long lValueToSend;
	portBASE_TYPE xStatus;

	xData *SendToQueue;
	SendToQueue = pvParameters;

	for(;;)
	{

		xStatus = xQueueSend(char_queue,SendToQueue,1000);

		if(xStatus != pdPASS)
		{

			printf("Could not send to the queue. \r\n");
		}
		taskYIELD();

	}

}
void task_reciverd(void *pvParameters)
{

	portBASE_TYPE xStatus;
	xData xReceivedchar;
	for(;;)
	{
		
		
		if(uxQueueMessagesWaiting(char_queue) != 5)

		{
			printf("Queue should have been full! \r\n");
		}
		xStatus = xQueueReceive(char_queue,&xReceivedchar,0);
		if(xStatus == pdPASS)
		{
			if(xReceivedchar.ucSource == send1_id )
			{
				printf("From Sender 1 = %d \r\n",xReceivedchar.ucValue);
		
			}
			if(xReceivedchar.ucSource == send2_id )
			{
				printf("From Sender 2 = %d \r\n",xReceivedchar.ucValue);
		
			}
		}
		else
		{

			printf("Could not receive from num_queue\r\n");
		}
	}
}
void task_periodic(void *pvParameters)
{

	for(;;)
	{
		vTaskDelay(250);

		printf("Periodic tasl - About to generate an interrupt \r\n");
		__asm{ int 0x82 }
		printf("Periodic task - Interrupt generated. \r\n\r\n\r\n");

	}

}
/*
void task_HandleInterrupt(void *pvParameters)
{

	for(;;)
	{
		xSemaphoreGive(xBinarySemaphore,300);

		printf("Handle task - Processing event \r\n");

	}
}

*/